package com.phone_verification_code;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @ClassName: ALiYunVerificationCodeForPhoneNumber
 * @Author: ZhangYue26
 * @Description: 阿里云手机验证码对接(我的免费次数已经使用完了，不能直接使用，需换成自己的账号进行使用)  TODO 购买短信：https://market.aliyun.com/products/56928004/cmapi027248.html?spm=5176.10695662.1996646101.searchclickresult.21934bf3C3ZhPX#sku=yuncode2124800000
 * @Date: 2020-10-09
 */
public class ALiYunVerificationCodeForPhoneNumber {
    public static void main(String[] args) throws Exception {
        //TODO 你只需要更改下面的三个参数即可，详情可以查看上面的网址链接
        String phone = "";//手机号
        String variable = "code:5678";//参数
        String templateId = "TP18040314";//模板ID

        URL url = new URL("http://yzxyzm.market.alicloudapi.com/yzx/verifySms?phone="+phone+"&variable="+variable+"&templateId="+templateId);
        String appcode = "ea2e3489d49a4065bda18bd7406f62a4";
        HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);//POST请求不能使用缓存
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
        urlConnection.setRequestProperty("Authorization","APPCODE " + appcode);
        urlConnection.setRequestMethod("POST");
        urlConnection.connect();
        InputStream inputStream = urlConnection.getInputStream();
        byte[] bytes = new byte[8 * 1024];
        while (inputStream.read(bytes) != -1) {
            System.out.println(new String(bytes, "UTF-8"));
            bytes = new byte[8 * 1024];
        }
    }
}
